package com.ysu.zyw.svc;

import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ysu.zyw.common.Constant;
import com.ysu.zyw.dao.UserMapper;
import com.ysu.zyw.entity.User;
import com.ysu.zyw.entity.UserExample;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public List<User> getAllUser() {
        List<User> userList = userMapper.selectByExample(null);
        userList.sort((Comparator.comparing((User user) -> user.getRole())).thenComparing(user -> user.getName()));
        return userList;
    }

    public String getAllProgramUserIdStr() {
        return getUserIdStr(Constant.ROLE_PROGRAM);
    }

    public String getAllAccountingUserIdStr() {
        return getUserIdStr(Constant.ROLE_ACCOUNTING);
    }

    public String getAllManagerUserIdStr() {
        return getUserIdStr(Constant.ROLE_MANAGER);
    }

    private String getUserIdStr(int roleType) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andRoleEqualTo(roleType);
        StringBuilder userIdStr = new StringBuilder();
        userMapper.selectByExample(userExample).stream().forEach(user -> userIdStr.append(user.getId() + ","));
        return userIdStr.substring(0, userIdStr.length() - 1);
    }
}
